// pages/specialSound/specialSound.js
import SpecialApi from '../../api/special.js'
import {parseTime,getPoint} from '../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        viewHeight: 50,
        timeSumLen:'00:00',
        timeNow:'00:00',
        loading:false,
        innerAudioContext:null,
        isStart:false,
        optionList:[],
        optionIndex:-1,
        optionObj:{},
        settingShow:false,
        paramSettingIndex:0,
        modelImageList:['f-1','f-2','f-3','f-4'],
        paramSettingList:[],
        selectImg:'',
        scrollId:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 页面首次渲染完毕时执行
        let height = wx.getSystemInfoSync().windowHeight
        let $navBar = this.selectComponent("#navBar")
        let navBarHeight = $navBar.data.navBarExtendHeight + $navBar.data.navBarHeight
        let viewHeight = height - navBarHeight
        this.setData({
            viewHeight: viewHeight
        })

        this.getSpecialSound(options.name)
        // if (options.firstMenuName == '肺和胸膜'){
        //     this.setData({
        //         modelImageList:['f-1','f-2','f-3','f-4']
        //     })
        // }else if(options.firstMenuName =='心脏检查'){
        //     this.setData({
        //         modelImageList:['x-1']
        //     })
        // }else {
        //     this.setData({
        //         modelImageList:['c-1']
        //     })
        // }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        //关闭所有播放
        if(this.data.innerAudioContext != null){
            this.data.innerAudioContext.stop()
            this.data.innerAudioContext.destroy()
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    handlerGobackClick(){
        //关闭所有播放
        if(this.data.innerAudioContext != null){
            this.data.innerAudioContext.stop()
            this.data.innerAudioContext.destroy()
        }
        wx.navigateBack()
    },
    /**
     * 播放音乐
     */
    play(){
        if(this.data.innerAudioContext == null){
            wx.showToast({
              title: '请选择听诊点',
              icon: 'none',
              duration: 2000
            })
            return
        }
        //修改状态
        const optionObj = this.data.optionObj
        optionObj.status = true
        const optionList = this.data.optionList
        optionList[this.data.optionIndex].status = true
        const paramSettingList = this.data.paramSettingList
        paramSettingList[this.data.paramSettingIndex].pointSoundsList[this.data.optionIndex].status = true
        this.setData({
            isStart:true,
            optionObj,
            optionList,
            paramSettingList
        })
        this.data.innerAudioContext.play()
    },
    /**
     * 暂停播放
     */
    pause(){
        this.data.innerAudioContext.pause()
        this.setData({
            isStart:false
        })
    },
    /**
     * 点击音原菜单
     */
    optionItemClick(e){
        const item = e.currentTarget.dataset.obj
        const index = e.currentTarget.dataset.index
        //选中当前点对应的图片
        this.setData({
            scrollId:item.image,
            optionObj:item,
            optionIndex:index,
            timeNow:'00:00',
            selectImg:item.image
        })
        this.initAudio()
        this.play()
    },
    /**
     * 初始化播放器
     */
    initAudio(){
        //关闭所有播放
        //销毁
        if(this.data.innerAudioContext != null){
            this.data.innerAudioContext.stop()
            this.data.innerAudioContext.destroy()
        }
        this.setData({
            loading:true
        })
        wx.showLoading({title: '音源加载中', icon: 'loading', duration: 10000});

        const innerAudioContext = wx.createInnerAudioContext({useWebAudioImplement:true})
        innerAudioContext.autoplay = false
        innerAudioContext.loop = true
        innerAudioContext.src = 'https://zhongyi.tellyescloud.com/resource/'+this.data.optionObj.url
        innerAudioContext.onPlay(() => {
            console.log('开始播放')
        })
        innerAudioContext.onError((res) => {
            console.log(res.errMsg)
            console.log(res.errCode)
        })
        innerAudioContext.onCanplay(()=> {
            console.log('初始化完成')
            wx.hideToast();
            // 必须。可以当做是初始化时长
            innerAudioContext.duration;
            // 必须。不然也获取不到时长
            setTimeout(() => {
                wx.hideLoading()
                this.setData({
                    timeSumLen:parseTime(innerAudioContext.duration * 1000,'{i}:{s}'),
                    loading:false
                })
            }, 200)
        })

        innerAudioContext.onTimeUpdate(()=>{
            console.log(innerAudioContext.currentTime)
            const time = innerAudioContext.currentTime.toString()
            const m = time.split('.')[0]
            const timeNow =parseTime(Number(m) * 1000,'{i}:{s}')
            this.setData({
                timeNow:timeNow,
            })
            // if(this.data.timeSumLen == this.data.timeNow && this.data.timeNow !==0){
            //     this.setData({
            //         isStart:false,
            //     })
            // }
        })

        this.setData({
            innerAudioContext:innerAudioContext,
            isStart:false
        })
    },
    /**
     * 点击参数设置
     */
    paramSettingClick(){
        this.setData({
            settingShow:true
        })
    },
    /**
     * 关闭参数设置模态看
     */
    onSettingClose(){
        this.setData({
            settingShow:false
        })
    },
    /**
     * 参数设置切换
     */
    screenItemChange(e){
        const index = e.currentTarget.dataset.index
        let imgList = []
        this.data.paramSettingList[index].pointSoundsList.forEach(item=>{
            if(!imgList.includes(item.image)){
                imgList.push(item.image)
            }
        })
        this.setData({
            scrollId:'',
            modelImageList:imgList,
            paramSettingIndex:index,
            optionList:this.data.paramSettingList[index].pointSoundsList,
            optionIndex:0,
            optionObj:this.data.paramSettingList[index].pointSoundsList[0],
            settingShow:false
        })
        this.initAudio()
    },
    async getSpecialSound(name){
        const res = await SpecialApi.getSpecialSound({name:name})
        res.data.forEach(item=>{
            item.pointSoundsList.forEach(point=>{
                const obj = getPoint(point.name)
                point.x = obj ? obj.x :''
                point.y = obj ? obj.y :''
                point.image = obj ? obj.img :''
            })
        })
        let imgList = []
        res.data[0].pointSoundsList.forEach(item=>{
            if(!imgList.includes(item.image)){
                imgList.push(item.image)
            }
        })
        this.setData({
            modelImageList:imgList,
            paramSettingList:res.data,
            optionList:res.data[0].pointSoundsList,
        })
    }

})
